package com.bdqn.t350.controller;

import com.bdqn.t350.pojo.SysUser;
import com.bdqn.t350.service.SysUserService;
import com.bdqn.t350.service.SysUserServiceImp;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping(path = "/hr")
public class HelloController {


    @Resource
    SysUserService sysUserService;


    @RequestMapping(path = "/hello")
    protected String hello(HttpServletRequest request, Model model) {
        System.out.println("SpringMVC 我来了");
        model.addAttribute("name", "zhangsan");
        //初始化spring容器
        // 从application作用域（ServletContext）获得spring容器
        //方式1： 手动从作用域获取 getAttribute返回的Object类型 需要强转
        ApplicationContext apppApplicationContext = (ApplicationContext) request.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        String[] beanDefinitionNames = apppApplicationContext.getBeanDefinitionNames();
        Arrays.sort(beanDefinitionNames);
        System.out.println(Arrays.asList(beanDefinitionNames));
        return "hello";
    }

    @RequestMapping(value = "/hello1")
    protected String hello1(Model model, @RequestParam String name, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("SpringMVC1 我来了");
        model.addAttribute("name", name);
        request.setAttribute("name1", name);
        HttpSession session = request.getSession();
        session.setAttribute("sessionName", name);
        return "hello";
    }

    @RequestMapping(value = "/list")
    protected String list(Model model) {
        System.out.println("list 我来了");
        List<SysUser> list = sysUserService.getListBiz();
        model.addAttribute("list", list);
        return "list";
    }
}
